<template>
  <div ref="workloadRef">
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="12">
          <a-form-item label="项目名称" v-bind="validateInfos.projectId">
            <j-search-select v-model:value="formData.projectId" dict="tm_project,project_name,id" :disabled="disabled" allowClear />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="所属部门" v-bind="validateInfos.departId">
            <j-search-select v-model:value="formData.departId" dict="sys_depart where org_type=2,depart_name,id" :disabled="disabled" allowClear />
          </a-form-item>
        </a-col>
        <!-- <a-col :span="24">
          <a-form-item  label="工时预估">
            <a-input v-model:value="formData.totalWorkload"  :disabled="true"></a-input>
          </a-form-item>
          :style="{fontWeight:'bold'}"
        </a-col> -->
        <a-col :span="7">
            <a-form-item label="专项测试">
              <a-checkbox-group v-model:value="formData.specialTestType" @change="specialTestTypeSelect">
              <a-checkbox value="performance" name="specialTestType">性能测试</a-checkbox>
              <a-checkbox value="automation" name="specialTestType">自动化测试</a-checkbox>
              </a-checkbox-group>
             </a-form-item>
        </a-col>
        <a-col :span="8">
            <a-form-item label="安全测试">
              <a-checkbox-group v-model:value="formData.safeTestType" @change="safeTestTypeSelect">
              <a-checkbox value="penetration" name="safeTestType">渗透测试</a-checkbox>
              <a-checkbox value="codeAudit" name="safeTestType">代码审计</a-checkbox>
              <a-checkbox value="componentAudit" name="safeTestType">组件审计</a-checkbox>
              </a-checkbox-group>
             </a-form-item>
        </a-col>
        <a-col :span="8">
            <a-form-item label="品控监理" >
              <a-checkbox-group v-model:value="formData.auditTestType" @change="auditTestTypeSelect">
              <a-checkbox value="auditBatch" name="auditTestType">批次性监理</a-checkbox>
              <a-checkbox value="auditStage" name="auditTestType">阶段性监理</a-checkbox>
              </a-checkbox-group>
             </a-form-item>
        </a-col>
        <!-- 工作量估算  -->
        <!-- <a-col :span="12">
            <a-form-item label="预估工作量测算">
              <div style="display:flex;">
              <a-input v-model:value="formData.totalWorkload"  :disabled="false"></a-input>
              <span style="width: 250px;color: red;" v-if="formData.updateTotalWorkload-formData.totalWorkload>0">
            （调增+
            {{formData.updateTotalWorkload-formData.totalWorkload}} 人/天）</span>
            <span style="width: 250px; color: green;" v-if="formData.updateTotalWorkload-formData.totalWorkload<0">
            （调减
            {{formData.updateTotalWorkload-formData.totalWorkload}} 人/天）</span>
          </div>
             </a-form-item>
        </a-col> -->
        <!-- <a-col :span="24">
          <a-form-item  label="工时预估">
            <a-input v-model:value="formData.totalWorkload"  :disabled="true"></a-input>
          </a-form-item>
          display:flex;align-items:center;justify-content:center;
        </a-col> -->
        <div style="margin-left:420px; font-size: 22px;color: blue;" v-if="formData.totalWorkload>0">
          <span style="margin-right:15px">质控预估工作量</span>{{formData.updateTotalWorkload}} 人/天 
          <span style="font-size: 15px;color: red;" v-if="formData.updateTotalWorkload-formData.totalWorkload>0">
            （调增+
            {{formData.updateTotalWorkload-formData.totalWorkload}} 人/天）</span>
            <span style="font-size: 15px;color: green;" v-if="formData.updateTotalWorkload-formData.totalWorkload<0">
            （调减
            {{formData.updateTotalWorkload-formData.totalWorkload}} 人/天）</span>
          </div>
        <a-divider style="margin-top:-1px;margin-bottom:5px">
          <!-- <div style="font-size: 22px;color: blue;" v-if="formData.totalWorkload>0">
            测算预计总工作量:{{formData.updateTotalWorkload}} 人/天 
          <span style="font-size: 22px;color: red;" v-if="formData.updateTotalWorkload-formData.totalWorkload>0">
            （调整+
            {{formData.updateTotalWorkload-formData.totalWorkload}} 人/天）</span>
            <span style="font-size: 22px;color: green;" v-if="formData.updateTotalWorkload-formData.totalWorkload<0">
            （调整
            {{formData.updateTotalWorkload-formData.totalWorkload}} 人/天）</span>
          </div> -->
          </a-divider>
        <div style="margin-bottom:10px;float:right;">
          <span style="font-size:16px;margin-left:920px;color:red;">*工作量测算结果仅供参考！ </span>
        </div>
        <a-col :span="24">
        <vxe-table
          border
          show-footer
          ref="tableRef"
          align="center"
          :height="'1270'"
          :loading="loading"
          :print-config="{}"
          :column-config="{ resizable: true, width: 90 }"
          :merge-cells="mergeCells"
          :merge-footer-items="mergeFooterItems"
          :footer-method="footerMethod"
          :show-overflow='true' :menu-config="tableMenu"
          :show-header-overflow="false"
          :data="tableData"
        >
          <vxe-column field="option1" title="项目活动" width="100">
          </vxe-column>
          <vxe-column field="option2" title="工作项" width="120" />
          <vxe-column field="option3" title="影响因子" width="150" style="display:flex;justify-content: flex-start;"/>
          <vxe-column field="factor1" title="原子参数[1]" width="150">
            <template #default="{ row }">

              <!-- <a-form-item label="项目名称"> -->
                <div style="display:flex; align-items: center; padding: 5px; width:105px;margin-right: 20px;">
                  <!-- <span>{{row.placeHolder}}</span> -->
                  <!-- style="width:60px;margin-left:10px;margin-right: 10px;" -->
                  <vxe-input
                  v-if="row.factors.includes('factor1') && row.showFlag"
                  @blur="calc(row)"
                  v-model="row.factor1"
                  :placeholder="row.placeHolder || '请输入参数'"
                  size="medium"
                />
                <span v-else>--</span>
                <span v-if="row.showFlag" style="marigin-left:20px">{{row.placeHolderTips}}</span>
              </div>
             
           <!-- </a-form-item> -->
             
              <!-- <span v-else>{{ row.factor1 }}</span> -->
            </template>
          </vxe-column>

          <vxe-column field="factor2" title="原子参数[2]" width="140">
            <template #default="{ row }">
              <!-- <span>{{row.placeHolder2}}</span> -->
              <div style="display:flex;justify-content: flex-start; align-items: center; padding: 5px; width:125px;margin-right: 20px;">
              <vxe-input v-if="row.factors.includes('factor2')  && row.showFlag" 
              @blur="calc(row)" v-model="row.factor2"  
              :placeholder="row.placeHolder2 || '请输入参数'" size="medium" />
              <!-- <span v-else>{{ row.factor2 }}</span> -->
              <span v-else>--</span>
              <span v-if="row.showFlag" style="marigin-left:20px">{{row.placeHolderTips2}}</span>
              </div>
            </template>
          </vxe-column>

          <!-- <vxe-column field="factor3" title="参数[3]" width="150">
      <template #default="{ row }">
        {{ row.factor3 }}
      </template>
    </vxe-column> -->
          <vxe-column field="forumlaRes" title="初算工作量(人/天)">
            <template #default="{ row }">
              <vxe-input v-if="row.showFlag" :disabled="true"
              @blur="updateFooter" v-model="row.forumlaRes" placeholder="0" type="number" :step="0.5" :precision="2" />
              <!-- {{ row.forumlaRes }} -->
              <span v-else>--</span>
            </template>
          </vxe-column>


          <!-- :cell-style="cellStyleMethod" -->
          <vxe-column field="modifyForumlaRes" title="调整工作量(人/天)" >
          <template #default="{ row }">
              <div style="display:flex;margin-right:5px;">
                <vxe-input v-if="row.showFlag" 
                @blur="updateFooter" v-model="row.modifyForumlaRes" @change="modifyForumlaResChange(row)" type="number" :step="0.5" :precision="2" placeholder="0"/>
                <span v-else>--</span>
              <!-- <span v-if="row.modifyForumlaRes > row.forumlaRes">
              <img style="width: 10px; height: 12px" src="./resource/up.png" alt="" />
              </span>
              <span v-else-if="row.modifyForumlaRes < row.forumlaRes">
                <img style="width: 10px; height: 12px" src="./resource/down.png" alt="" />
              </span>  -->
            </div>
              <!-- {{ row.forumlaRes }} -->
            </template>
          </vxe-column>

          <vxe-column field="forumlaRes" title="预估工作量(人/天)">
            <template #default="{ row }">
              <div style="display:flex;margin-right:5px;">
              <vxe-input v-if="row.showFlag"  :disabled="true"
              @blur="updateFooter" v-model="row.finalForumlaRes" placeholder="0" type="number" :step="0.5" :precision="2" />
              <!-- {{ row.forumlaRes }} -->
              <span v-else>--</span>
              <span v-if="row.finalForumlaRes > row.forumlaRes">
              <img style="width: 10px; height: 12px" src="./resource/up.png" alt="" />
              </span>
              <span v-else-if="row.finalForumlaRes < row.forumlaRes">
                <img style="width: 10px; height: 12px" src="./resource/down.png" alt="" />
              </span> 
            </div>
            </template>
          </vxe-column>

          <vxe-column field="modifyReason" title="调整原因" width="260" style="color:red">
          <template #default="{ row }">
              <div style="color:red;">
                <vxe-input v-if="row.showFlag && row.finalForumlaRes!= row.forumlaRes" 
                 v-model="row.modifyReason" placeholder="请输入调整原因" type="string"/>
            </div>
              <!-- {{ row.forumlaRes }} -->
            </template>
          </vxe-column>
          <!-- <vxe-column field="desc" title="评估影响因素分析" width="250" /> -->

        </vxe-table>
      </a-col>
      </a-row>
    </a-form>
  </a-spin>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  // import { defineComponent,defineProps, reactive, ref, nextTick } from 'vue';
  import { VXETable,VxeTableInstance, VxeTablePropTypes } from 'vxe-table';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JSearchSelect from '/@/components/Form/src/jeecg/components/JSearchSelect.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../ProjectWorkloadAssessment.api';
  import { Form } from 'ant-design-vue';
  import { json } from 'stream/consumers';
  import html2canvas from 'html2canvas';

  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => {} },
    formBpm: { type: Boolean, default: true },
  });
  const formRef = ref();
  const workloadRef = ref();
  // const workloadRef = ref<HTMLElement | null>(null);

  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    projectId: '',
    departId: '',
    workloadItem: null,
    totalWorkload: '',
    updateTotalWorkload: '',
    specialTestType:'',
    safeTestType:'',
    auditTestType: ''
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({style: { width: '100px' }})
  const wrapperCol = ref<any>({});
  // const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  // const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //测试准备
  const testPrepareFlag = ref<boolean>(true);
  //测试执行
  const testExecuteFlag = ref<boolean>(true);
  const obj = ref({
    performance: false,
    automation: false
  })
  const specialTestList = ['performance', 'automation'];
  const safeTestList = ['penetration', 'codeAudit', 'componentAudit'];
  //性能
  const performanceFlag = ref<boolean>(false);
   //自动化
  const automationFlag = ref<boolean>(false);
  //渗透测试
  const penetrationFlag = ref<boolean>(false);
  //代码审计
  const codeAuditFlag = ref<boolean>(false);
  //组件审计
  const componentAuditFlag = ref<boolean>(false);
  const auditBatchFlag = ref<boolean>(false);
  const auditStageFlag = ref<boolean>(false);

  const validatorRules = {
    projectId: [{ required: true, message: '请选择项目!' }],
    departId: [{ required: true, message: '请选择所属部门!' }],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  async function  savePageAsImage() {
      try {
        const element = workloadRef.value //document.body; // 要保存为图片的页面元素 //
        const canvas = await html2canvas(element, {
          scrollX: -window.scrollX +100,
          scrollY: -window.scrollY +100,
          windowWidth: document.documentElement.scrollWidth ,
          windowHeight: document.documentElement.scrollHeight+ 100
        });
        const img = canvas.toDataURL('image/png');
        const imgElement = document.createElement('img');
        imgElement.src = img;
 
        // 创建下载链接
        const a = document.createElement('a');
        a.href = img;
        a.download = '工作量测算.png';
        a.click();
      } catch (error) {
        console.error('Error saving page as image:', error);
      }
    }

  function cellStyleMethod({ row, column }) {
    // if (row.name === '特定值') {
    //   return 'background-color: yellow; color: red;';
    // } else {
    //   return 'background-color: #fcfcfc; color: #666;';
    // }
    return 'background-color: yellow; color: red;';
  }
  function changeLog(row) {
    alert('changeLog')
  }
  
  function modifyForumlaResChange(row) {
    console.log('modifyForumlaResChange:',row)
    console.log('row.forumlaRes:',row.forumlaRes)
    if(row.forumlaRes == undefined) {
      row.forumlaRes = 0
      row.finalForumlaRes = parseFloat(row.forumlaRes) + parseFloat(row.modifyForumlaRes)
      console.log('finalForumlaRes:',row.finalForumlaRes)
      // if(row.finalForumlaRes<0) {
      //   return
      // }
    //   手动更新统计数据
  
    } else {
      row.finalForumlaRes = parseFloat(row.forumlaRes) + parseFloat(row.modifyForumlaRes)
      // row.modifyForumlaRes = ''
    }
    nextTick(() => {
      console.log(tableRef);
      tableRef.value.updateFooter(true);
    });
   
  }
  // 表单禁用
  const disabled = computed(() => {
    if (props.formBpm === true) {
      if (props.formData.disabled === false) {
        return false;
      } else {
        return true;
      }
    }
    return props.formDisabled;
  });

  /**
   * 新增
   */
  function add() {
    // edit({});
  }

  function specialTestTypeSelect() {
    performanceFlag.value = formData.specialTestType.includes('performance')
    automationFlag.value = formData.specialTestType.includes('automation')
  }
  function safeTestTypeSelect() {
    //渗透测试
    penetrationFlag.value = formData.safeTestType.includes('penetration')
    //代码审计
    codeAuditFlag.value = formData.safeTestType.includes('codeAudit')
    //组件审计
    componentAuditFlag.value = formData.safeTestType.includes('componentAudit')
  }
  function auditTestTypeSelect() {
    auditBatchFlag.value = formData.auditTestType.includes('auditBatch')
    auditStageFlag.value = formData.auditTestType.includes('auditStage')
  }
  
  /**
   * 编辑
   */
  function edit(record) {
    console.log('record:',JSON.stringify(record));
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if (record.hasOwnProperty(key)) {
          tmpData[key] = record[key];
        }
      });
      console.log(tmpData, 'tmpData');

      //赋值
      Object.assign(formData, tmpData);

      tableData.value = JSON.parse(formData.workloadItem, (key, value) => {
        if (typeof value === 'string' && value.startsWith('(row)')) {
          return new Function('return ' + value)();
        }
        return value;
      });
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化

    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    formData.workloadItem = JSON.stringify(tableData.value, (key, value) => {
      if (typeof value === 'function') {
        return value.toString();
      }
      return value;
    });
    // formData.workloadItem = JSON.stringify(tableData.value);
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }

  const mergeCells = [
    // // 第一列合并
    { row: 0, col: 0, rowspan: 6, colspan: 1 }, //测试准备
    { row: 6, col: 0, rowspan: 3, colspan: 1 }, //测试执行

    { row: 9, col: 0, rowspan: 7, colspan: 1 }, //专项测试
    { row: 16, col: 0, rowspan: 6, colspan: 1 }, //安全测试
    { row: 22, col: 0, rowspan: 2, colspan: 1 }, //品质监理

    // // 第二列合并
    { row: 0, col: 1, rowspan: 2, colspan: 1 }, //需求分析
    { row: 2, col: 1, rowspan: 2, colspan: 1 }, //测试案例
    { row: 4, col: 1, rowspan: 2, colspan: 1 },  //测试数据
    { row: 6, col: 1, rowspan: 1, colspan: 1 },  //测试轮数
    { row: 7, col: 1, rowspan: 1, colspan: 1 },  //案例数预估
    { row: 8, col: 1, rowspan: 1, colspan: 1 },  //测试缓冲量

    { row: 9, col: 1, rowspan: 4, colspan: 1 },  //性能测试
    { row: 13, col: 1, rowspan: 3, colspan: 1 },  //自动化脚本
    { row: 16, col: 1, rowspan: 4, colspan: 1 }, //渗透测试 
    // { row: 16, col: 1, rowspan: 1, colspan: 1 },  //代码审计
    // { row: 16, col: 1, rowspan: 1, colspan: 1 }, //组件审计
    // { row: 24, col: 1, rowspan: 2, colspan: 1 }, //组件审计


    // // 末列合并
    // // 222134
    // { row: 0, col: 6, rowspan: 2, colspan: 1 },
    // { row: 2, col: 6, rowspan: 2, colspan: 1 },
    // { row: 4, col: 6, rowspan: 2, colspan: 1 },
    // { row: 6, col: 6, rowspan: 1, colspan: 1 },
    // { row: 7, col: 6, rowspan: 3, colspan: 1 },
    // { row: 10, col: 6, rowspan: 4, colspan: 1 },
  ];
  const mergeFooterItems = [
    { row: 0, col: 0, rowspan: 1, colspan: 5 },
    // { row: 0, col: 6, rowspan: 1, colspan: 3 },
    // { row: 0, col: 6, rowspan: 1, colspan: 3 },

    // { row: 0, col: 0, rowspan: 1, colspan: 4 },
    // { row: 0, col: 5, rowspan: 1, colspan: 3 },

    // { row: 0, col: 0, rowspan: 1, colspan: 2 },
    // { row: 0, col: 2, rowspan: 1, colspan: 2 },
    // { row: 0, col: 4, rowspan: 1, colspan: 2 },
    // { row: 0, col: 6, rowspan: 1, colspan: 2 },
  ];
  //   公式1  A*C
  const forumla1 = (row) => {
    const { factor1, factor2, factor3 } = row;
    return parseFloat(Number(factor1 || 0) * Number(factor3 || 0)).toFixed(2);
  };
//   公式1  B*C
  const forumlaBXC = (row) => {
    const { factor1, factor2, factor3 } = row;
    return parseFloat(Number(factor2 || 0) * Number(factor3 || 0)).toFixed(2);
  };
   //   公式1  A*B*C
   const forumlaAxB = (row) => {
    const { factor1, factor2, factor3 } = row;
    return parseFloat(Number(factor1 || 0) * Number(factor2 || 0) * Number(factor3 || 0)).toFixed(2);
  };
  //   公式2  A/B
  const forumla2 = (row) => {
    const { factor1, factor2, factor3 } = row;
    // if (Number(factor3 || 0) === 0) return '';
    return parseFloat((Number(factor1 || 0) / Number(factor3 || 1)).toFixed(2));
  };
  //   公式3  A
  const forumla3 = (row) => {
    const { factor1, factor2, factor3 } = row;
    return parseFloat(factor1).toFixed(2);
  };
  //   公式4  A/B*C
  const forumla4 = (row) => {
    console.log('forumla4:公式4  A/B*C')
    const { factor1, factor2, factor3 } = row;
    return  parseFloat((Number(factor1 || 0) / Number(factor2 || 1)) * Number(factor3 || 0)).toFixed(2);
  };
  //   公式5  B
  const forumla5 = (row) => {
    const { factor1, factor2, factor3 } = row;
    return parseFloat(factor2).toFixed(2);
  };
  //   公式6  C
  const forumla6 = (row) => {
    const { factor1, factor2, factor3 } = row;
    return parseFloat(factor3).toFixed(2);
  };
  //   公式7  A+B
  const forumla7 = (row) => {
    const { factor1, factor2 } = row;
    return parseFloat(Number(factor1 || 0) + Number(factor2 || 0)).toFixed(2);
  };
  const forumlaAudit = (row) => {
    const { factor1, factor3 } = row;
    return Number(factor1 || 0) * Number(factor3 || 0);
  };
  
  //   公式8  参考：>=3个系统系数1.0、>=6个系统系数2.0、>=10个系统系数3.0,增加[1.0,3.0]系数
  const forumla8 = (row) => {
    const { factor1, factor2, factor3 } = row;
    if (Number(factor1 || 0) >= 3 && Number(factor1 || 0) < 6) {
      return '1.0';
    } else if (Number(factor1 || 0) >= 6 && Number(factor1 || 0) < 10) {
      return '2.0';
    } else if (Number(factor1 || 0) >= 10) {
      return '3.0';
    }
    return '0';
  };

  const tableData = ref([
    {
      desc: '与测试人数相关',
      option1: '测试准备',
      option2: '需求分析',
      option3: '需求文档分析',
      factorRange: [0.5,1],
      factor3: '0.5', //0.5-1
      forumla: forumla1,
      placeHolder: '测试人数',
      placeHolderTips: '人',
      factors: ['factor1', 'factor3'],
      showFlag:testPrepareFlag,
    },
    {
      desc: '与测试人数相关',
      option1: '测试准备',
      option2: '需求分析',
      option3: '测试方案设计',
      factorRange: [0.5,1],
      factor3: '0.5', //0.5-1
      forumla: forumla1,
      placeHolder: '测试人数',
      placeHolderTips: '人',
      factors: ['factor1', 'factor3'],
      showFlag:testPrepareFlag,
    },
    {
      desc: '与测试案例总数相关',
      option1: '测试准备',
      option2: '测试案例',
      option3: '测试案例编写',
      factorRange: [1,1],
      factor3: '1', 
      forumla: forumlaAxB,
      placeHolder: '测试人数',
      placeHolder2: '编写案例天数',
      placeHolderTips: '人',
      placeHolderTips2: '天',
      factors: ['factor1', 'factor2', 'factor3'],
      showFlag:testPrepareFlag,
    },
    {
      desc: '与测试案例总数相关',
      option1: '测试准备',
      option2: '测试案例',
      option3: '测试案例评审',
      factorRange: [0.5,0.1],
      factor3: '0.5', //0.5-0.1
      forumla: forumlaBXC,
      placeHolder2: '案例评审天数',
      placeHolderTips2: '天',
      factors: ['factor2', 'factor3'],
      showFlag:testPrepareFlag,
    },
    {
      desc: '中心平均效率一般在0.5~1人天',
      option1: '测试准备',
      option2: '测试数据',
      option3: '基础数据准备',
      factorRange: [0.5,1],
      factor3: '0.5',//0.5-1
      forumla: forumla6,
      factors: ['factor3'],
      showFlag:testPrepareFlag,
    },
    {
      desc: '中心平均效率一般在0.5~1人天',
      option1: '测试准备',
      option2: '测试数据',
      option3: '业务数据准备',
      factorRange: [0.5,1],
      factor3: '0.5',//0.5-1
      forumla: forumla6,
      factors: ['factor3'],
      showFlag:testPrepareFlag,
    },
    {
      desc: '与轮数、测试案例总数、人均执行案例数相关',
      option1: '测试执行',
      option2: '测试轮数',
      option3: '测试轮数',
      factorRange: [],
      factor3: '',
      forumla: forumla3,
      placeHolder: '测试轮数',
      placeHolderTips: '轮',
      factors: ['factor1'],
      showFlag:testPrepareFlag,
    },
    {
      desc: '与轮数、测试案例总数、人均执行案例数相关',
      option1: '测试执行',
      option2: '案例数预估',
      option3: '人均每天执行数',
      factorRange: [1,2],
      factor3: '1', //1-2
      forumla: forumla4,
      placeHolder: '案例预估数',
      placeHolder2: '日执行案例',
      placeHolderTips: '个',
      placeHolderTips2: '个/人天',
      factors: ['factor1', 'factor2', 'factor3'],
      showFlag:testExecuteFlag
    },
    {
      desc: '与轮数、测试案例总数、人均执行案例数相关',
      option1: '测试执行',
      option2: '测试缓冲量',
      option3: '系统总数',
      factorRange: [],
      factor3: '1',
      forumla: forumla8,
      placeHolder: '系统数量',
      placeHolderTips: '个',
      factors: ['factor1'],
      showFlag:testExecuteFlag
    },
    {
      desc: '性能脚本编写、调试、执行',
      option1: '专项测试',
      option2: '性能测试',
      option3: '方案设计&评审',
      factorRange: [0.5,1],
      factor3: '0.5',//0.5-1
      forumla: forumla1,
      placeHolder: '测试人数',
      placeHolderTips: '人',
      factors: ['factor1', 'factor3'],
      showFlag: performanceFlag
    },

    {
      desc: '性能脚本编写、调试、执行',
      option1: '专项测试',
      option2: '性能测试',
      option3: '脚本编写数',
      factorRange: [1,1],
      factor3: '1',
      forumla: forumla4,
      placeHolder: '脚本总数',
      placeHolder2: '日编写脚本',
      placeHolderTips: '个',
      placeHolderTips2: '个/人天',
      factors: ['factor1', 'factor2', 'factor3'],
      showFlag: performanceFlag //formData.specialTestType.includes('performance')
      // factors: ['factor1', 'factor3'],
    },
    {
      desc: '性能脚本编写、调试、执行',
      option1: '专项测试',
      option2: '性能测试',
      option3: '场景压测数',
      factorRange: [1,1],
      factor3: '1',
      forumla: forumla4,
      placeHolder: '场景总数',
      placeHolder2: '日压测场景',
      placeHolderTips: '个',
      placeHolderTips2: '个/人天',
      factors: ['factor1', 'factor2', 'factor3'],
      showFlag: performanceFlag
    },
    {
      desc: '性能脚本编写、调试、执行',
      option1: '专项测试',
      option2: '性能测试',
      option3: '铺底数据准备',
      factorRange: [0.5,1],
      factor3: '0.5',//0.5-1
      forumla: forumla6,
      factors: ['factor3'],
      showFlag:performanceFlag
    },
        {
      desc: '与接口、UI、场景数量相关',
      option1: '专项测试',
      option2: '自动化脚本',
      option3: '脚本编写',
      factorRange: [1,1],
      factor3: '1',
      forumla: forumla4,
      placeHolder: '脚本总数',
      placeHolder2: '日编写脚本',
      placeHolderTips: '个',
      placeHolderTips2: '个/人天',
      factors: ['factor1', 'factor2', 'factor3'],
      showFlag:automationFlag
    },
    {
      desc: '与接口、UI、场景数量相关',
      option1: '专项测试',
      option2: '自动化脚本',
      option3: '脚本调试',
      factorRange: [1,1],
      factor3: '1',
      forumla: forumla4,
      placeHolder: '脚本总数',
      placeHolder2: '日调试脚本',
      placeHolderTips: '个',
      placeHolderTips2: '个/人天',
      factors: ['factor1', 'factor2', 'factor3'],
      showFlag:automationFlag
    },
    {
      desc: '与接口、UI、场景数量相关',
      option1: '专项测试',
      option2: '自动化脚本',
      option3: '脚本执行',
      factorRange: [1,1],
      factor3: '1',
      forumla: forumla4,
      placeHolder: '脚本总数',
      placeHolder2: '日执行脚本',
      placeHolderTips: '个',
      placeHolderTips2: '个/人天',
      factors: ['factor1', 'factor2', 'factor3'],
      showFlag:automationFlag
    },
    {
      desc: '-',
      option1: '安全测试',
      option2: '渗透测试',
      option3: 'APP（Andriod&iOS)',
      factorRange: [],
      factor3: '0',
      forumla: forumla7,
      placeHolder: '初测',
      placeHolder2: '复测',
      placeHolderTips: '人天',
      placeHolderTips2: '人天',
      factors: ['factor1', 'factor2', 'factor3'],
      showFlag:penetrationFlag
    },
    {
      desc: '-',
      option1: '安全测试',
      option2: '渗透测试',
      option3: 'Web',
      factorRange: [],
      factor3: '0',
      forumla: forumla7,
      placeHolder: '初测',
      placeHolder2: '复测',
      placeHolderTips: '人天',
      placeHolderTips2: '人天',
      factors: ['factor1', 'factor2', 'factor3'],
      showFlag:penetrationFlag
    },
    {
      desc: '-',
      option1: '安全测试',
      option2: '渗透测试',
      option3: '公众号',
      factorRange: [],
      factor3: '0',
      forumla: forumla7,
      placeHolder: '初测',
      placeHolder2: '复测',
      placeHolderTips: '人天',
      placeHolderTips2: '人天',
      factors: ['factor1', 'factor2', 'factor3'],
      showFlag:penetrationFlag
    },
    {
      desc: '-',
      option1: '安全测试',
      option2: '渗透测试',
      option3: '小程序',
      factorRange: [],
      factor3: '0',
      forumla: forumla7,
      placeHolder: '初测',
      placeHolder2: '复测',
      placeHolderTips: '人天',
      placeHolderTips2: '人天',
      factors: ['factor1', 'factor2', 'factor3'],
      showFlag:penetrationFlag
    },
    {
      desc: '-',
      option1: '安全测试',
      option2: '代码审计',
      option3: '源代码（含人工审计）',
      factorRange: [],
      factor3: '0',
      forumla: forumla7,
      placeHolder: '初测',
      placeHolder2: '复测',
      placeHolderTips: '人天',
      placeHolderTips2: '人天',
      factors: ['factor1', 'factor2', 'factor3'],
      showFlag:codeAuditFlag
    },
    {
      desc: '-',
      option1: '安全测试',
      option2: '组件审计',
      option3: '源代码（含人工审计）',
      factorRange: [],
      factor3: '0',
      forumla: forumla7,
      placeHolder: '初测',
      placeHolder2: '复测',
      placeHolderTips: '人天',
      placeHolderTips2: '人天',
      factors: ['factor1', 'factor2', 'factor3'],
      showFlag:componentAuditFlag
    },
    // 
    {
      desc: '-',
      option1: '品控监理',
      option2: '批次性监理',
      option3: '审计与监理',
      factorRange: [5,10],
      factor3: '5',
      forumla: forumlaAudit,
      placeHolder: '批次数量',
      placeHolderTips: '个',
      factors: ['factor1','factor3'],
      showFlag:auditBatchFlag
    },

    {
      desc: '-',
      option1: '品控监理',
      option2: '阶段性监理',
      option3: '审计与监理',
      factorRange: [15,20],
      factor3: '15',
      forumla: forumlaAudit,
      placeHolder: '系统数量',
      placeHolderTips: '个',
      factors: ['factor1','factor3'],
      showFlag:auditStageFlag
    },
  ]);
  const tableRef = ref(null);
  // 计算
  function calc(row) {
    console.log('rowrowrow:',row);
    //   console.log(row.forumlaRes);
    //   手动更新统计数据
    nextTick(() => {
      const { forumla, option1, option2, option3 } = row;
      row.forumlaRes = forumla(row);
      row.modifyForumlaRes = 0;
      row.finalForumlaRes = Number(row.forumlaRes) + Number(row.modifyForumlaRes);
      console.log(tableRef);
      tableRef.value.updateFooter(true);
    });
  }
  function updateFooter() {
    // const $table = this.$refs.tableRef;
    // const tableRef = ref(null);
    nextTick(() => {
      console.log(tableRef);
      tableRef.value.updateFooter(true);
  });
  }
  function footerMethod({ columns, data }) {
    // console.log('columns:',columns)
    // console.log('data:',data)
    const sums: string[] = [];
    let sumCell;
    columns.forEach((column, columnIndex) => {
      // console.log('field:',column.field);
      if (columnIndex === 0) {
        sums.push('测算工作量');
      } else if (columnIndex === 5) {
       
      } else {
        let modifySumCell;
        let finalForumlaRes;
        //   if (column.field === 'forumlaRes') {
        // sumCell = sumNum(data, column.field);
        sumCell = Number(sumNum(data, 'forumlaRes'));
        sums.push('初算:'+sumCell);
        modifySumCell = Number(sumNum(data, 'modifyForumlaRes'));
        // console.log('typeof(modifySumCell):',typeof(modifySumCell))
        finalForumlaRes = Number(sumCell) + Number(modifySumCell);
        // console.log(finalForumlaRes)
        formData.totalWorkload = sumCell;
        formData.updateTotalWorkload = finalForumlaRes;
        //   }
        if(formData.updateTotalWorkload-formData.totalWorkload>0){
          // sums.push('<span style="color: red;">调增+:'+modifySumCell+'</span>');
          sums.push('调增:+'+modifySumCell);
        } else if(formData.updateTotalWorkload-formData.totalWorkloal<0){
          sums.push('调减:'+modifySumCell);
        } else {
          sums.push('');
        }
       
        sums.push('预估:'+finalForumlaRes);
        sums.push('');

        // sums.push(sumCell);
        // sums.push('调整工作量:'+modifySumCell);
        //   }
      }
    });
    // 返回一个二维数组的表尾合计
    return [sums];
  }
  const sumNum = (list, field: string) => {
    // console.log('sumNum field:' ,field)
    let count:number = 0.0;
    list.forEach((item) => {
      // console.log(i,'-sumNum item[:' ,field,'] value:',Number(item[field]) || 0)
      count += Number(item[field]) || 0;
    });
    return count.toFixed(2);
    // return `${count}`;
  };
  function submit() {
    let params = {};
    saveOrUpdate(params, false).then((res) => {
      console.log(res);
    });
  }

  defineExpose({
    add,
    edit,
    submitForm,
    savePageAsImage
  });
</script>

<style lang="less" scoped>
/* 选择到a-form-item的label并加粗 */
  .ant-form-item label {
    font-weight: bold;
  }

  .antd-modal-form {
    height: 2000 !important;
    overflow-y: auto;
    padding: 14px;
  }

  .vxe-input--default .vxe-input--inner input {
  color: #e20505; /* 你想要的颜色 */
}
</style>
